package com.bzzoneit.newsys.web.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bzzoneit.newsys.entity.admin.Menu;
import com.bzzoneit.newsys.entity.admin.User;
import com.bzzoneit.newsys.service.admin.MenuService;
import com.bzzoneit.newsys.service.admin.UserService;
import com.bzzoneit.newsys.utils.Constant;
import com.bzzoneit.newsys.utils.dwz.AjaxDone;
import com.bzzoneit.newsys.utils.dwz.JsonResult;

@Controller
public class MainController extends BaseController {

	@Autowired
	private UserService userService;

	@Autowired
	private MenuService menuService;

	

	/**m
	 * 登录首页
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/index")
	public String index(Model model) {
		User user = new User();
		user.setId(1L);
		user = userService.login(user);
		model.addAttribute("foo", user.getName());
		List<Menu> list = menuService.getMainMenuList(user.getId());
	    long pid=list.get(0).getId(); //获得第一个大菜单的id
		model.addAttribute("list", list);
		List<Menu> leftlist = menuService.getSubMenuList(user.getId(), pid);	
		model.addAttribute("leftlist", leftlist);
		return "index";
	}

	
	@RequestMapping("/main")
	public String main(Model model) throws Exception {
		List<Menu> list = menuService.getMainMenuList(1L);
		for(Menu m:list){
			m.setTreestring(menuService.getTreeStringByParentId(1L, m.getId(),false));
		}
		model.addAttribute("list", list);
		return "main";
	}
	
	@ResponseBody
	@RequestMapping("/tree/{pid}")
	public List<Menu> tree(@PathVariable("pid") Long pid) {	
		List<Menu> list=menuService.getListByParentId(1L, pid);
		return list;
	}	
	
	/**
	 *	退出登录
	 * 
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request) {
		//request.getSession().invalidate();//清除所有 session 方便还是清除自己需要的 
		HttpSession s=request.getSession();
		s.removeAttribute(Constant.USERID);
		s.removeAttribute(Constant.PASSWORD);
		s.removeAttribute(Constant.USERS);
		return "redirect:login";
	}
	
	@RequestMapping("/change")
	@ResponseBody
	public AjaxDone change(HttpServletRequest request,@RequestParam("newPassword") String newPassword) {
		HttpSession s=request.getSession();
		User user =(User) s.getAttribute(Constant.USERS);
		user.setPassword(newPassword);
		userService.updateUser(user);
		s.removeAttribute(Constant.USERID);
		s.removeAttribute(Constant.PASSWORD);
		s.removeAttribute(Constant.USERS);
		AjaxDone a=new AjaxDone();
		a.setStatusCode(200);
		a.setCallbackType("forward");
		a.setMessage("修改密码成功,必须退出重新登录");
		a.setForwardUrl("login"); 
		a.setNavTabId("main"); //重新加载这个nav
		return a;
	}
	/**
	 * 登录页面
	 * 
	 * @return
	 */
	@RequestMapping("/login")
	public String login() {
		return "login";
	}
	
	@RequestMapping("/changepassword")
	public String changepassword() {
		return "changepassword";
	}
	
	@RequestMapping("/enter")
	@ResponseBody
	public JsonResult  enter(
			HttpServletRequest request,
			@RequestParam("username") String username,@RequestParam("password") String password,
			@RequestParam("randcode") String randcode) {
		HttpSession s=request.getSession();
		JsonResult result=new JsonResult();
		//获得验证码比较
		String code = (String)request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		User u=userService.login(username, password);
		if(!code.equals(randcode)){
			result.setMsg("验证码错误");
			result.setSuccess(false);
		}
		else if(u==null){
			result.setMsg("用户不存在");
			result.setSuccess(false);
		}
		else{
			s.setAttribute(Constant.USERID, u.getId());
			s.setAttribute(Constant.USERS, u);
			s.setAttribute(Constant.PASSWORD, password);
			result.setMsg("登录成功");
			result.setSuccess(true);
		}
		return result;
	}

	@RequestMapping("/login_dialog")
	public String login_dialog() {

		return "login_dialog";
	}

	@RequestMapping(value = "/sidebar/{id}")
	public String prepare(Model model, @PathVariable("id") String id) {
		User user = new User();
		user.setId(1L);
		List<Menu> leftlist = menuService.getSubMenuList(user.getId(),Long.parseLong(id));	
		model.addAttribute("leftlist", leftlist);
		return "sidebar/list";
	}

	@RequestMapping(value = "/sidebar")
	public String sidebar(Model model, @RequestParam("name") String name) {
		System.out.println("start");
		model.addAttribute("foo", "测试" + name);
		return "sidebar/list";
	}

}
